package com.qdlc.p2p.biz.service.impl;

import com.qdlc.p2p.biz.service.LinkService;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.LinkDao;
import com.qdlc.p2p.dal.dto.Link;
import com.qdlc.p2p.dal.model.LinkModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hongbinglin on 2016/6/3.
 */
@Service("linkService")
public class LinkServiceImpl implements LinkService {

    @Resource
    private LinkDao linkDao;


    @Override
    public Link findById(long id) {
        return linkDao.find(id);
    }

    @Override
    public PageDataList<Link> findLinkList(LinkModel model) {
        QueryParam param = QueryParam.getInstance();
        if(model.getStatus() > 0 ){
            param.addParam("status", model.getStatus());
        }
        if(model.getType() > 0){
            param.addParam("type", model.getType());
        }
        if(StringUtil.isNotEmpty(model.getSearchName())){
            param.addParam("title", SearchFilter.Operators.LIKE, model.getSearchName().trim());
        }
        param.addOrder(OrderFilter.OrderType.DESC, "sort");
        param.addOrder(OrderFilter.OrderType.DESC, "id");
        param.addPage(model.getPage(), model.getRows());
        return linkDao.findPageList(param);
    }

    @Override
    public Link save(Link link) {
        return linkDao.save(link);
    }

    @Override
    public Link update(Link link) {
        return linkDao.update(link);
    }

    @Override
    public List<Link> findByType(int type) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("type", type);
        param.addParam("status", 1);
        param.addOrder(OrderFilter.OrderType.DESC, "sort");
        param.addOrder(OrderFilter.OrderType.DESC, "id");
        return linkDao.findByCriteria(param);
    }

}
